{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}

<div class="layui-col-lg12 layui-col-md12 layui-col-sm12">

    <div class="">
        <form class="layui-form gg-form-bar border-t border-x">
            <div class="layui-input-inline" style="width:300px;">
                <input type="text" name="id" placeholder="ID" class="layui-input" autocomplete="off" />
            </div>
             <div class="layui-input-inline" style="width:300px;">
                <input type="text" name="bar_code" placeholder="物料编码" class="layui-input" autocomplete="off" />
            </div>
             <div class="layui-input-inline" style="width:300px;">
                <input type="text" name="mdName" placeholder="物料名称" class="layui-input" autocomplete="off" />
            </div>
            <div class="layui-input-inline" style="width:300px;">
                <input type="text" name="specs" placeholder="规格型号" class="layui-input" autocomplete="off" />
            </div>
            <div class="layui-input-inline" style="width:300px;">
                <input type="text" id="orderDate" readonly name="orderDate" placeholder="单据日期" class="layui-input" autocomplete="off" />
            </div>
            <div class="layui-input-inline" style="width:300px;">
                <select name="status" >
                    <option value="">所有状态</option>
                    {foreach ['','草稿','已确认']  as $key=>  $v}
                    <option value="{$key}">{$v}</option>
                    {/foreach}
                </select>
            </div>

            <div class="layui-input-inline" style="width:150px;">
                <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webformIndex"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
                <button type="reset" class="layui-btn layui-btn-reset" lay-filter="reset">清空</button>
            </div>
        </form>
        <div class="p-3">
            <div>
                <table class="layui-hide" id="treeTable" lay-filter="treeTable"></table>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="toolbarDemo">
    <a class="layui-btn layui-btn-sm add-menu" lay-event="add">+ 添加成本</a>
<!--    <button class="layui-btn layui-btn-sm layui-btn-primary" id="import" >导入</button>-->
<!--    <a href="{__EXPORTMODE__}/成本导入模板.xlsx" download="" class="layui-btn layui-btn-sm layui-btn-primary">导入模板</a>-->
    <a class="layui-btn layui-btn-sm layui-btn-danger " lay-event="dels" >删除</a>
    <a class="layui-btn layui-btn-sm layui-btn-normal " target="_blank" href="{:url('exportData')}">导出</a>
</script>
<script type="text/html" id="actionBar">
    <a class="layui-btn layui-btn-normal  layui-btn-xs"  lay-event="info">详情</a>
    <a class="layui-btn layui-btn-normal  layui-btn-xs"  lay-event="edit">编辑</a>
<!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
    const moduleInit = ['tool'];
    var mStatus=['','草稿','已确认'];
    function gouguInit() {
        var treeTable = layui.treeTable, tool = layui.tool,form = layui.form,laydate=layui.laydate;
        var upload=layui.upload;
        var tableIns = treeTable.render({
            elem: '#treeTable'
            , url: "{:url('index')}"
            ,toolbar: '#toolbarDemo',
            defaultToolbar:  ['filter',  'exports']
            , tree: { // treeTable 特定属性集
                customName: {name: 'name'},
                data: {},
                view: {showIcon: false},
                async: {},
                callback: {}
            }
            , cellMinWidth: 80
            , cols: [[
                {type: 'checkbox', fixed: 'left' },
                {fixed: 'left',field: 'id',title: 'ID' ,unresize: true,hide:true},
                {field: 'id', width: 80, title: 'ID号', align: 'center'}
                , {field: 'orderDate',  title: '单据日期'}
                , {field: 'countPrice',  title: '分摊费用合计(元)'}
                 , {field: 'status', title: '状态',  align: 'center',templet:function (d) {
                        return mStatus[d.status];
                    }}
                , {title: '操作',fixed: 'right',width: 200,  align: 'center'
                    , toolbar: '#actionBar'
                }
            ]]
            , page: true
        });
        //监听搜索提交
        form.on('submit(webformIndex)', function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });
        //表头工具栏事件
        // $('.add-menu').on('click', function () {
        //     tool.side("{:url('add')}");
        //     return;
        // });

        //操作按钮
        treeTable.on('tool(treeTable)', function (obj) {

            if (obj.event === 'edit') {
                tool.side("{:url('add')}?id=" + obj.data.id);
                return;
            }
            if (obj.event === 'info') {
                tool.side("{:url('info')}?id=" + obj.data.id);
                return;
            }
            if (obj.event === 'del') {
                layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                    let callback = function (e) {
                        layer.msg(e.msg);
                        if (e.code == 0) {
                            obj.del();
                        }
                    }
                    tool.delete("{:url('delete')}", {id: obj.data.id}, callback);
                    layer.close(index);
                });
            }
            if(obj.event === 'forbid'){
                layer.confirm('确定要禁用吗?', {icon: 3, title:'提示'}, function(index){
                    let callback = function (e) {
                        layer.msg(e.msg);
                        if (e.code == 0) {
                            tableIns.reload();
                        }
                    }
                    tool.post("{:url('reuse')}", { id: obj.data.id,status: 2}, callback);
                    layer.close(index);
                });
            }
            if(obj.event === 'resume'){
                layer.confirm('确定要启用吗?', {icon: 3, title:'提示'}, function(index){
                    let callback = function (e) {
                        layer.msg(e.msg);
                        if (e.code == 0) {
                            tableIns.reload();
                        }
                    }
                    tool.post("{:url('reuse')}", { id: obj.data.id,status: 1}, callback);
                    layer.close(index);
                });
            }
        });
        treeTable.on('toolbar(treeTable)', function(obj){
            var checkStatus = treeTable.checkStatus(obj.config.id);
            console.log(checkStatus)
            switch(obj.event){
                case 'add':
                    tool.side("{:url('add')}");
                    break;
                case 'dels':
                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.id)
                        })
                        layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                            let callback = function (e) {
                                layer.msg(e.msg);
                                if (e.code == 0) {
                                    tableIns.reload();
                                }
                            }
                            tool.delete("{:url('delete')}", {id: ids.join(',')}, callback);
                            layer.close(index);
                        });

                    }else {
                        layer.msg('请选择数据');
                    }

                    break;
                case 'import':

                    break;

            };
        })
       var loadingIndex='';
        upload.render({
            elem: '#import'
            ,url: "{:url('importData')}"
            ,accept: 'file'
            ,exts: 'xlsx',
            before:function (res) {
                loadingIndex=layer.load(1,{
                    shade: [0.5,'#a09b9b']
                });
            }
            ,done: function(res){
                layer.msg(res.msg)
                layer.close(loadingIndex)
                setTimeout(function () {
                    location.reload()
                },2000)
            },
            error(e){
                layer.msg('网络异常请稍后再试')
                layer.close(loadingIndex)
            }
        });

        laydate.render({
            elem: '#orderDate'
            ,type: 'date',
            range:"~"
        });
    }



</script>
{/block}
<!-- /脚本 -->